#include<bits/stdc++.h>
using namespace std;
#define mkp make_pair
#define fir first
#define sec second
const int N = 2e5+5;
int n,q,c;
int lp,rp;
int a[N];
int lst[N][20];
int cnt[1<<6][1<<9];
vector<int>e[N];
vector<int>id[N],op[N];
int dfn[N],fa[N],ed[N],dfcnt;
int S[N];
pair<int,int>p[20];
void dfs(int u)
{
    dfn[u] = ++dfcnt;
    if(u == 1) for(int i = 0;i < c;i++) lst[u][i] = 1;
    else
    {
        for(int i = 0;i < c;i++) lst[u][i] = lst[fa[u]][i];
        lst[u][a[fa[u]]] = u;
    }
    for(auto v:e[u]) dfs(v);
    ed[u] = dfcnt;
}
int ans[N];
void Ins(int x,int y)
{
    int lv = x&((1<<lp)-1);
    int atlv = ((1<<lp)-1)^lv;
    int rv = x>>lp;
    cnt[lv][rv]+=y;
    for(int i = atlv;i;i = (i-1)&atlv) cnt[lv^i][rv]+=y;
}
int Qry(int x)
{
    int lv = x&((1<<lp)-1);
    int rv = x>>lp,res = 0;
    for(int i = rv;i;i = (i-1)&rv) res += cnt[lv][i];
    return res + cnt[lv][0];
}
int main()
{
    freopen("tree.in","r",stdin);
    freopen("tree.out","w",stdout);
    ios::sync_with_stdio(false);cin.tie(0);
    cin>>n>>q>>c;
    for(int i = 1;i <= n;i++) cin>>a[i];
    for(int i = 2,f;i <= n;i++)
    {
        cin>>fa[i];
        e[fa[i]].push_back(i);
    }
    dfs(1);
    lp = min(6,c);
    rp = c-lp;
    for(int i = 1;i <= q;i++)
    {
        int u;cin>>S[i]>>u;
        if(!(S[i]&(1<<a[u])))
        {
            ans[i] = 1;
            continue;
        }
        int rt = 1;
        for(int j = 0;j < c;j++) if(!(S[i]&(1<<j))) rt = max(rt,lst[u][j]);
        id[dfn[rt]-1].push_back(-i);
        id[ed[rt]].push_back(i);
        //assert((1<<a[rt])&S[i]);
        //cout<<rt<<" "<<lst[u][0]<<"\n";
    }
    for(int i = 1;i <= n;i++)
    {
        for(int j = 0;j < c;j++)
        {
            if(a[i] != j) p[j] = mkp(fa[lst[i][j]],j);
            else p[j] = mkp(-1,j);
        }
        sort(p,p+c);
        int cur = (1<<c)-1;
        for(int j = 1;j < c;j++)
        {
            op[dfn[p[j].fir]].push_back(cur);
            cur ^= 1<<p[j].sec;
            op[dfn[p[j].fir]].push_back(-cur);
        }
        op[dfn[i]].push_back(cur);
    }
    for(int i = 1;i <= n;i++)
    {
        for(auto x:op[i])
        {
            if(x > 0) Ins(x,1);
            else Ins(-x,-1);
        }
        for(auto x:id[i])
        {
            if(x > 0) ans[x] += Qry(S[x]);
            else ans[-x] -= Qry(S[-x]);
        }
    }
    for(int i = 1;i <= q;i++) cout<<ans[i]<<"\n";
    cerr<<clock()<<"\n";
    return 0;
}